package com.ym.medical.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Coder;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.CounsellingRoomDao;
import com.ym.medical.dao.InquiryOrderDao;
import com.ym.medical.domain.entity.CommentRecordEntity;
import com.ym.medical.domain.entity.CounsellingRoomEntity;
import com.ym.medical.domain.entity.InquiryOrderEntity;
import com.ym.medical.domain.param.CounsellingRoomParam;
import com.ym.medical.domain.vo.CounsellingRoomRecordVO;
import com.ym.medical.domain.vo.DoctorPraisedVo;
import com.ym.medical.service.ICommentRecordSV;
import com.ym.medical.service.ICounsellingRoomSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * counselling_room
 *
 * @author 625 2019-04-25
 */
@Service
public class CounsellingRoomSVImpl extends BaseSVImpl<CounsellingRoomEntity,CounsellingRoomDao> implements ICounsellingRoomSV {

	@Autowired
	private InquiryOrderDao inquiryOrderDao;

	@Autowired
	private ICommentRecordSV commentRecordSV;

	/**
	 * 更新
	 */
	@Override
	public void update(CounsellingRoomEntity counsellingRoom) {
		this.updateById(counsellingRoom);
	}

	/**
	 * Load查询
	 */
	@Override
	public CounsellingRoomEntity load(long id) {
		return this.selectById(id);
	}

	@Override
	public PageInfo<CounsellingRoomEntity> listPage(CounsellingRoomParam counsellingRoom, int pageNum, int pageSize) {
		if (null == counsellingRoom) {
			throw new BusinessException(Coder.System.FAIL);
		}
		PageHelper.startPage(pageNum, pageSize);
		List<CounsellingRoomEntity> users = this.selectList(counsellingRoom.covert());
		PageInfo<CounsellingRoomEntity> pageInfo = new PageInfo(users);
		return pageInfo;
	}

	@Override
	public PageInfo<CounsellingRoomRecordVO> listPageDoctorVisitRecord(CounsellingRoomParam counsellingRoom, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<CounsellingRoomRecordVO> pageInfo = new PageInfo(listDoctorVisitRecord(counsellingRoom));
		return pageInfo;
	}

	@Override
	public List<CounsellingRoomRecordVO> listDoctorVisitRecord(CounsellingRoomParam counsellingRoom) {
		List<CounsellingRoomRecordVO> counsellingRoomRecordVOS = mapper.listDoctorVisitRecord(counsellingRoom);
		return counsellingRoomRecordVOS;
	}

	@Override
	public PageInfo<CounsellingRoomRecordVO> listPagePatientVisitRecord(CounsellingRoomParam counsellingRoom, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<CounsellingRoomRecordVO> pageInfo = new PageInfo(listPatientVisitRecord(counsellingRoom));
		return pageInfo;
	}

	@Override
	public List<CounsellingRoomRecordVO> listPatientVisitRecord(CounsellingRoomParam counsellingRoom) {
		List<CounsellingRoomRecordVO> counsellingRoomRecordVOS = mapper.listPatientVisitRecord(counsellingRoom);
		return counsellingRoomRecordVOS;
	}

	/**
	 * 医生优评信息
	 *
	 * @param doctorId
	 * @return
	 */
	@Override
	public DoctorPraisedVo evaluate(Long doctorId) {
		DoctorPraisedVo doctorPraised = new DoctorPraisedVo();
		//咨询人数
		int count = getOrderCount(doctorId);
		doctorPraised.setConsultNum(count);
		//好评率
		BigDecimal goodRate = getGoodRate(doctorId);
		doctorPraised.setGoodRate(goodRate);
		return doctorPraised;
	}

	/**
	 * 查询该医生的好评率
	 *
	 * @param doctorId
	 * @return
	 */
	public BigDecimal getGoodRate(Long doctorId) {
		List<CommentRecordEntity> commentRecords = commentRecordSV.selectList(CommentRecordEntity.builder().commentAppperson(doctorId).build());
		BigDecimal results = new BigDecimal(0);
		if (commentRecords.size() <= 0) {
			return null;
		}
		for (CommentRecordEntity commentRecord : commentRecords) {
			/*拿到每一条的星数*/
			Integer starnumber = commentRecord.getCommentStarnumber();
			/*计算每一条的好评率*/
			BigDecimal number = new BigDecimal(starnumber);
			BigDecimal result = number.divide(new BigDecimal(5), 2, BigDecimal.ROUND_HALF_UP);
			results = results.add(result);
		}
		return results.divide(new BigDecimal(commentRecords.size()), 2, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 获取该用户下的所有订单总数
	 *
	 * @param doctorId
	 * @return
	 */
	public int getOrderCount(Long doctorId) {
		return inquiryOrderDao.selectCount(InquiryOrderEntity.builder().userId(doctorId).build());
	}

	@Override
	public CounsellingRoomEntity findByModel(CounsellingRoomEntity counsellingRoomModel) {
		return this.selectOne(counsellingRoomModel);
	}


	@Override
	public List<JSONObject> getOrderInfoByRoomId(Long roomId) {
		return mapper.getOrderInfoByRoomId(roomId);
	}

	@Override
	public PageInfo<CounsellingRoomRecordVO> theInquiryList(CounsellingRoomParam model, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<CounsellingRoomRecordVO> pageInfo = new PageInfo(mapper.theInquiryList(model));
		return pageInfo;
	}

	@Override
	public CounsellingRoomRecordVO inquiryInfo(CounsellingRoomParam model) {
		return mapper.inquiryInfo(model);
	}

	@Override
	public PageInfo<CounsellingRoomRecordVO> patientConsultationRecord(CounsellingRoomParam model, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<CounsellingRoomRecordVO> pageInfo = new PageInfo(mapper.patientConsultationRecord(model));
		return pageInfo;
	}


}
